﻿using System.Reflection;
using System.Web.Mvc;
using Autofac;
using Autofac.Extras.DynamicProxy2;
using Hyt.Core;
using Huayuan.ApplicationService;
using Huayuan.Manage.Ioc;

namespace Huayuan.Manage
{
    public class IocSetting : IIocSetting
    {
        public void Register(Autofac.ContainerBuilder builder, IUnitOfWork unitOfWork)
        {
            new Impl_Hyt_Core().Register(builder, unitOfWork);
            new Impl_Hyt_Passport_Persist().Register(builder, unitOfWork);
            new Impl_Hyt_Passport_Service().Register(builder, unitOfWork);
            new Impl_Hyt_Web().Register(builder, unitOfWork);
            new Impl_Hyt_Web_Mvc().Register(builder, unitOfWork);

            RegisterControllers(builder);
            RegisterServiceCenter(builder, unitOfWork);
        }

        /// <summary>
        /// 注册控制器
        /// </summary>
        /// <param name="builder"></param>
        public virtual void RegisterControllers(ContainerBuilder builder)
        {
            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(t => typeof(IController).IsAssignableFrom(t)).PropertiesAutowired();
        }


        /// <summary>
        /// 注册ServiceCenter
        /// </summary>
        /// <param name="builder"></param>
        public virtual void RegisterServiceCenter(ContainerBuilder builder, IUnitOfWork unitOfWork)
        {
            builder.RegisterType<ServiceCenter>().PropertiesAutowired().OnActivating(t => 
                t.Instance.SetUnitOfWork(unitOfWork)
                ).EnableClassInterceptors();
        }
    }
}
